package org.zachary.commonservice.strategy.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.zachary.commonmodule.entity.MqTransmissionJson;
import org.zachary.commonservice.entity.CreditScoreStrategy;
import org.zachary.commonservice.entity.ServiceType;
import org.zachary.commonservice.mapper.CreditScoreStrategyMapper;
import org.zachary.commonservice.strategy.CreditStrategy;
import org.zachary.commonservice.strategy.cache.StrategyCache;

@Component
public class HospitalServiceStrategy implements CreditStrategy {
    @Autowired
    private CreditScoreStrategyMapper strategyMapper;
    @Autowired
    private StrategyCache cache;


    @Override
    public ServiceType getServiceType() {
        return ServiceType.ComfortableMedicalTreatment;
    }

    @Override
    public int calculate(MqTransmissionJson message, int currentScore) {
        // 1. 加载配置（基于用户当前信用分）
        CreditScoreStrategy config = loadConfig(message.getServiceType(), currentScore);
        if (config == null) {
            throw new IllegalStateException("未找到匹配的信用策略配置");
        }

        if (message.getIsOverdue()) {
            return -config.getPenaltyByPeriod(message.getOverduePeriod());
        }
        else {
            double baseAmount = config.getAmount() > 0 ? config.getAmount() : 1.0;
            int amountRatio = (int) (message.getAmount() / baseAmount);
            System.out.println("加分分值："+amountRatio);
            return (config.getAddScore() + amountRatio);
        }
    }

    private CreditScoreStrategy loadConfig(Integer serviceTypeCode, int userScore) {
        ServiceType serviceType = ServiceType.values()[serviceTypeCode];
        return cache.getStrategy(serviceType, userScore);
    }
}
